package com.dalianbank.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.dalianbank.pojo.AllTables;
import com.dalianbank.pojo.GzhdCpResult;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;

public class SerchController extends Controller {
	public void index(){
		
	}
	public void searchCP(){
		String where="";
		Map<String,String> smap=new HashMap<String,String>();
		String paramslist=getPara("paramslist");
		if(paramslist!=null){
		String []parpms=paramslist.split(",");
		for(String str:parpms){
			smap.put(str, getPara(str));
	 	 }
		   where=getSQLCondition(smap);
		   System.out.println(where);
		}
	 
		String eoName=getPara("eoName");
		String rows=getPara("rows");
		String page=getPara("page");
	//	String JA10DATE=getPara("GZHD_CP_RESULT.JA10DATE");
	
	    int start=Integer.parseInt(rows)*(Integer.parseInt(page)-1)+1;
	    int end=Integer.parseInt(rows)*(Integer.parseInt(page));
		GzhdCpResult gzhd=getModel(GzhdCpResult.class,eoName);
		String sql="select * from (select A.*,rownum rn from (select * from vp.GZHD_CP_RESULT where 1=1 "+where+")a  where rownum<="+end+") where rn>="+start;
		List<GzhdCpResult> list=GzhdCpResult.me.find(sql);
		BigDecimal count=Db.queryBigDecimal("select count(*) from vp.GZHD_CP_RESULT");
		Map map=new HashMap();
		map.put("total", count);
		map.put("rows", list);
		renderJson(map);
	}
	 private String getSQLCondition(Map<String,String> map){
		 String sql="";
		for(String s:map.keySet()){
			if(map.get(s)!=null&&!map.get(s).equals("")){
				if(s.equals("GZHD_CP_RESULT.JA10DATE")){
					sql+=" and "+s+" = '"+formatData(map.get(s))+ "'";	
				}else{
					sql+=" and "+s+" = '"+map.get(s)+ "'";
				}
			
			}
		}
		 return sql;
	 }
	 private String formatData(String str){
		 String [] strlist=str.split("-");
		 if(strlist.length==3){
			 if(strlist[1].length()==1){
				 strlist[1]="0"+strlist[1];
			 }
			 if(strlist[2].length()==1){
				 strlist[2]="0"+strlist[2];
			 }
			 str=strlist[0]+strlist[1]+strlist[2];
			
		 }else{
			 
		 }
		//str.replaceAll("-", "");
		 return str;
	 }
	 

}
